import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
//1.引入element-plus组件库
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
//3.引入element-plus图标库
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
//5.1修改elementPlus默认中文
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

//8.1配置根路径   js的常量命名格式全大写    装到window中变成全局变量，而window里本身就有这么个变量
const BASE_URL = 'http://localhost:9100';
window.BASE_URL = BASE_URL;     //WINDOW里配置的都是全局变量，可以在任意javaScript标签内部使用，让js用


const app = createApp(App)  //2时定义的app
//8.2 配置根路径     在vue实例app中也添加全局属性BASE_URL，这样上面的template标签中也可以使用根路径，让html用    比如调用格数据库的图片，写路径
app.config.globalProperties.BASE_URL = BASE_URL;

//4.遍历ElementPlusIconsVue对象中的所有图标组件并注册到app中
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

//2.app.use(ElementPlus) 使用Element-plus组件库
//5.2 app.use(ElementPlus,{ locale: zhCn })将El本地化为中文
app.use(ElementPlus,{ locale: zhCn }).use(store).use(router).mount('#app')

//6.解决ResizeObserver Error
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
        let context = this;
        let args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    }
}
const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
        callback = debounce(callback, 16);
        super(callback);
    }
}

//7.定义获取用户信息的全局方法
window.getUser = ()=>{
    return localStorage.user ? JSON.parse(localStorage.user) : null ;
}






